<?php
$title="Order List";
$css="orderList.css";
include_once 'common/AdminTop.php';
include_once 'authentic.php';
include_once '../Class/DB.php';
$db=new DB();
$numPerPage=5;
$totalPage=ceil($db->get_number_of_order()/$numPerPage);
$page=$_GET['page']; 
if (!isset($page)) {
 $page=1;
} 
if($page>$totalPage)
{
	$page--;
}
if($page<=0)
{
	$page=1;
}
$startCount=($page-1)*$numPerPage; 
$meta=$_GET["meta"];
if(!isset($meta))
{
	$meta="oid";
}
$updown=$_GET["updown"];
if(!isset($updown))
{
$updown="ASC";
}
$orders=$db->get_sort_orders($meta, $updown, $startCount, $numPerPage);
?>
<div id="userlist" align="center"> 
<br>
<span style="color:red">Click an order id to edit,click an attribute to sort the list.</span>
<table id="userlistTable" border="1" style="margin-top:20px;font-size:12pt;text-align: center;">
	<tbody>
		<tr style="color:white;">
		    <th width=""><p><a href="orderList.php?page=1&meta=<?php echo "oid";?>&updown=<?php echo $updown=="ASC"?"DESC":"ASC";?>">Order Id</a></p></th>
			<th width="120px"><p><a href="orderList.php?page=1&meta=<?php echo "p_name";?>&updown=<?php echo $updown=="ASC"?"DESC":"ASC";?>">Name</a></p></th>
			<!-- <th width="65px"><a href="orderList.php?page=1&meta=<?php //echo "price";?>&updown=<?php //echo $updown=="ASC"?"DESC":"ASC";?>">price</a></th> -->
		    <th width="45px"><p><a href="orderList.php?page=1&meta=<?php echo "count";?>&updown=<?php echo $updown=="ASC"?"DESC":"ASC";?>">Count</a></p></th> 
			<th width="64px"><p><a href="orderList.php?page=1&meta=<?php echo "state";?>&updown=<?php echo $updown=="ASC"?"DESC":"ASC";?>">State</a></p></th>
			<th width="60px"><p><a href="orderList.php?page=1&meta=<?php echo "uid";?>&updown=<?php echo $updown=="ASC"?"DESC":"ASC";?>">UserName</a></p></th>
			<th width="82px"><p><a href="orderList.php?page=1&meta=<?php echo "should_pay";?>&updown=<?php echo $updown=="ASC"?"DESC":"ASC";?>">Should Pay</a></p></th>
			<th width=""><p><a href="orderList.php?page=1&meta=<?php echo "order_date";?>&updown=<?php echo $updown=="ASC"?"DESC":"ASC";?>">Order time</a></p></th>
			<th width=""><p><a href="orderList.php?page=1&meta=<?php echo "pay_date";?>&updown=<?php echo $updown=="ASC"?"DESC":"ASC";?>">Pay time</a></p></th>
			<th width="30px"></th>
		</tr>
		<?php 
		$i=0;
		while ($orders[$i]){
			$oneProduct=$db->get_pid_product($orders[$i]["pid"]);
			$orderman=$db->get_uid_info($orders[$i]["uid"]);
			switch ($orders[$i]["state"])
			{
				case 1:$orderState="Not Paid";break;
				case 2:$orderState="Paid";break;
				case 3:$orderState="Delivered";break;
				case 4:$orderState="Received";break;
			}
			$orderDate=substr($orders[$i]["order_date"], 4,2)."-".substr($orders[$i]["order_date"], 6,2)." ".substr($orders[$i]["order_date"], 8,2).":".substr($orders[$i]["order_date"], 10,2);
			$payDate=substr($orders[$i]["pay_date"], 4,2)."-".substr($orders[$i]["pay_date"], 6,2)." ".substr($orders[$i]["pay_date"], 8,2).":".substr($orders[$i]["pay_date"], 10,2);
		?>
		<tr>
			<td><p><a href="modifyorder.php?id=<?php echo $orders[$i]["oid"];?>&page=<?php echo $page;?>&updown=<?php echo $updown;?>&meta=<?php echo $meta;?>"><?php echo $orders[$i]["oid"];?></a></p></td>
			<td style="word-break:break-all;"><p><?php echo $oneProduct[0]["p_name"];?></p></td>
			<!--<td>￥<?php //echo $oneProduct[0]["act_price"];?></td>-->
	        <td><p><?php echo $orders[$i]["count"];?></p></td>
			<td><p><?php echo $orderState;?></p></td>
			<td><p><?php echo $orderman[0]["userName"];?></p></td>
			<td><p>$<?php echo number_format($orders[$i]["should_pay"],2,'.','');?></p></td>
			<td><p><?php echo $orderDate;?></p></td>
			<td><p><?php echo $orders[$i]["pay_date"]=="201212312359"?"":$payDate;?></p></td>
			<td><p><a href="orderDelete.php?meta=<?php echo $meta;?>&updown=<?php echo $updown;?>&id=<?php echo  $orders[$i]["oid"]?>&page=<?php echo $page;?>">
				<img alt="x" src="../image/delete.png" width="14">
			</a></p></td>
		</tr>
		<?php 
		$i++;
		}
		?>
		</tbody>
</table>
		<div id="page">
			<?php if ($page != 1) { ?>
			<a href="orderList.php?meta=<?php echo $meta;?>&updown=<?php echo $updown;?>&page=<?php echo $page - 1;?>">Back</a> 
			<?php }
			for ($i=1;$i<=$totalPage;$i++) {  
				if($i==$page){
					?>
					<p><?php echo $i;?></p>
			<?php continue;}
			?>
			<a href="orderList.php?meta=<?php echo $meta;?>&updown=<?php echo $updown;?>&page=<?php echo $i;?>"><?php echo $i ;?></a>
			<?php
			}
			if ($page<$totalPage) { 
			?>
				<a href="orderList.php?meta=<?php echo $meta;?>&updown=<?php echo $updown;?>&page=<?php echo $page + 1;?>">Next</a>
			<?php
					} 
			?>
			
			</div>
</div>
<?php 
include_once 'common/AdminTail.php';
?>